﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Perseus;

namespace Hetymine.Executioner.Controls {
    /// <summary>
    /// Interaction logic for CommandSelector.xaml
    /// </summary>
    public partial class CommandSelector : UserControl {
        private string[] _SpecialFolders;
        private bool _IsApply = true;
        private bool _IsSelect = false;

        public CommandSelector() {
            InitializeComponent();

            ComboBoxItem cbi;

            cbi = new ComboBoxItem();
            cbi.Content = "Administration Tools";
            this.cmbSpecialFolders.Items.Add(cbi);            

            cbi = new ComboBoxItem();
            if (PEnvironment.IsWindowsVistaOrAbove) {
                cbi.Content = "Documents";
            }
            else {
                cbi.Content = "My Documents";
            }
            this.cmbSpecialFolders.Items.Add(cbi);

            cbi = new ComboBoxItem();
            cbi.Content = "Fonts";
            this.cmbSpecialFolders.Items.Add(cbi);

            cbi = new ComboBoxItem();
            // Vista and above dropped the my name.
            if (PEnvironment.IsWindowsVistaOrAbove) {
                cbi.Content = "Computer";
            }
            else {
                cbi.Content = "My Computer";
            }
            this.cmbSpecialFolders.Items.Add(cbi);

            cbi = new ComboBoxItem();
            cbi.Content = "Control Panel";
            this.cmbSpecialFolders.Items.Add(cbi);

            cbi = new ComboBoxItem();
            cbi.Content = "Network Neighbourhood";
            this.cmbSpecialFolders.Items.Add(cbi);

            cbi = new ComboBoxItem();
            cbi.Content = "Recycle Bin";
            this.cmbSpecialFolders.Items.Add(cbi);

            cbi = new ComboBoxItem();
            cbi.Content = "Tasks Scheduler";
            this.cmbSpecialFolders.Items.Add(cbi);

            this.cmbSpecialFolders.SelectedIndex = 0;

            this._SpecialFolders = new string[] {
                "/N,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\\::{D20EA4E1-3957-11d2-A40B-0C5020524153}",
                "/N,::{450D8FBA-AD25-11D0-98A8-0800361B1103}",
                "/N,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\\::{D20EA4E1-3957-11d2-A40B-0C5020524152}",
                "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}",
                "/N,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}",
                "/N,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}",
                "/N,::{645FF040-5081-101B-9F08-00AA002F954E}",
                "/N,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\\::{D6277990-4C6A-11CF-8D87-00AA0060F5BF}"
            };

            this.cmbSpecialFolders.SelectionChanged += new SelectionChangedEventHandler(SpecialFolders_SelectionChanged);
            this.txtCommand.TextChanged += new TextChangedEventHandler(CommandInfo_TextChanged);
            this.txtArguments.TextChanged += new TextChangedEventHandler(CommandInfo_TextChanged);
            this.radCustom.Click += new RoutedEventHandler(CommandType_Click);
            this.radSpecial.Click += new RoutedEventHandler(CommandType_Click);
        }

        private void CommandType_Click(object sender, RoutedEventArgs e) {
            if (this._IsApply) {
                this.OnCommandChanged(this, new EventArgs());
            }
        }

        private void SpecialFolders_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (this._IsApply && this.radSpecial.IsChecked.Value) {
                this._IsSelect = true;
                this.txtCommand.Text = "Explorer";
                this.txtArguments.Text = this._SpecialFolders[this.cmbSpecialFolders.SelectedIndex];
                this._IsSelect = false;
                this.OnCommandChanged(this, new EventArgs());
            }
        }
        private void CommandInfo_TextChanged(object sender, TextChangedEventArgs e) {
            if (this._IsApply && !this.radSpecial.IsChecked.Value) {
                this.OnCommandChanged(this, new EventArgs());
            }
        }

        public string Command {
            get {
                if (this.radSpecial.IsChecked.Value) {
                    return "Explorer";
                }
                else {
                    return this.txtCommand.Text;
                }
            }
            set {
                this._IsApply = false;

                this.txtCommand.Text = value;

                if (!this._IsSelect) {
                    if (this.IsSpecialFolder) {
                        int index = value.IndexIn(this._SpecialFolders);
                        this.cmbSpecialFolders.SelectedIndex = index;

                        radSpecial.IsChecked = true;
                    }
                    else {
                        radCustom.IsChecked = true;
                    }
                }

                this._IsApply = true;

                if (!this._IsSelect) {
                    this.OnCommandChanged(this, new EventArgs());
                }
            }
        }
        public string Arguments {
            get {
                if (this.radSpecial.IsChecked.Value) {
                    return this._SpecialFolders[this.cmbSpecialFolders.SelectedIndex];                    
                }
                else {
                    return this.txtArguments.Text;
                }
            }
            set {
                this._IsApply = false;

                this.txtArguments.Text = value;

                if (!this._IsSelect) {
                    if (this.IsSpecialFolder) {
                        int index = value.IndexIn(this._SpecialFolders);
                        this.cmbSpecialFolders.SelectedIndex = index;

                        radSpecial.IsChecked = true;
                    }                    
                    else {
                        radCustom.IsChecked = true;
                    }
                }

                this._IsApply = true;

                if (!this._IsSelect) {
                    this.OnCommandChanged(this, new EventArgs());
                }
            }
        }

        public bool IsSpecialFolder {
            get {
                if (this.txtCommand.Text == "Explorer" && this._SpecialFolders.Contains(this.txtArguments.Text)) {
                    return true;
                }

                return false;
            }
        }

        public event EventHandler CommandChanged;
        protected virtual void OnCommandChanged(object sender, EventArgs e) {
            if (this.CommandChanged != null) {
                this.CommandChanged(sender, e);
            }
        }
    }
}
